from controllers.Base import HttpController,g_xcmsDict
import json
from utils.Utils import xc_gen_random_code
from settings import g_settings


class XcmsRecordTimelineController(HttpController):

    async def get(self, *args, **kwargs):
        data = await self.do()
        self.write(data)

    async def do(self):
        ret = False
        msg = "unknown error"
        info = {}
        request_params = self.request_get_params()
        safe = request_params.get('safe',"").strip()

        if safe == g_settings.appKey + g_settings.appSecret:
            try:
                xcms_name = request_params.get('xcms_name', "").strip()  # xcms节点
                record_code = request_params.get("record_code", "").strip()
                timestamp = int(request_params.get("timestamp"))

                val = g_xcmsDict.get(xcms_name)
                if not val:
                    raise Exception("the current xcms node does not exist")

                ws = val.get("ws")
                message_type = "CmdXcmsRecordTimeline"
                message_uuid = xc_gen_random_code()
                __cmd_data = {
                    "message_type": message_type,
                    "message_uuid": message_uuid,
                    "xcms_name": xcms_name,
                    "record_code": record_code,
                    "timestamp": timestamp
                }
                ws.send_cmd(__cmd_data)

                info["message_type"] = message_type
                info["message_uuid"] = message_uuid
                ret = True
                msg = "success"
            except Exception as e:
                msg = str(e)

        else:
            msg = "unsafe"

        res = {
            "code": 1000 if ret else 0,
            "msg": msg,
            "info": info
        }

        return json.dumps(res)

class XcmsRecordFilenameController(HttpController):

    async def get(self, *args, **kwargs):
        data = await self.do()
        self.write(data)

    async def do(self):
        ret = False
        msg = "unknown error"
        info = {}
        request_params = self.request_get_params()
        safe = request_params.get('safe',"").strip()

        if safe == g_settings.appKey + g_settings.appSecret:
            try:
                xcms_name = request_params.get('xcms_name', "").strip()  # xcms节点
                record_code = request_params.get("record_code", "").strip()
                timestamp = int(request_params.get("timestamp"))

                val = g_xcmsDict.get(xcms_name)
                if not val:
                    raise Exception("the current xcms node does not exist")

                fs_url = "%s/xcnvs/fs/record/upload"%g_settings.fsHttpHost
                ws = val.get("ws")
                message_type = "CmdXcmsRecordFilename"
                message_uuid = xc_gen_random_code()
                __cmd_data = {
                    "message_type": message_type,
                    "message_uuid": message_uuid,
                    "xcms_name": xcms_name,
                    "record_code": record_code,
                    "timestamp": timestamp,
                    "fs_url":fs_url
                }
                ws.send_cmd(__cmd_data)

                info["message_type"] = message_type
                info["message_uuid"] = message_uuid
                ret = True
                msg = "success"
            except Exception as e:
                msg = str(e)
        else:
            msg = "unsafe"

        res = {
            "code": 1000 if ret else 0,
            "msg": msg,
            "info": info
        }

        return json.dumps(res)
